using Unity.Netcode;
using UnityEngine;

namespace Since.Netcode
{
	public class NetcodeTransformAuthority : INetworkAuthority<NetcodeTransform>
	{
		public override void OnNetworkSpawn()
		{
			// 初始化位置和速度
			Vector3 initialPosition = owner.transform.position;

			// 更新网络数据
			owner.UpdateNetworkPosition(initialPosition);
			owner.UpdateNetworkVelocity(owner._Velocity.Value);

			owner.SyncState();
		}


		public override void Update()
		{
			float deltaTime = Time.deltaTime;

			// 获取当前速度
			Vector3 velocity = owner.NetworkVelocity;

			// 使用velocity移动对象
			var newPos = owner.transform.position + velocity * deltaTime;
			owner.transform.position = newPos;

			// 更新网络数据中的位置
			owner.UpdateNetworkPosition(newPos);
		}
	}
}
